package movenment2copy;
import java.awt.List;
import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;



/**
 * This class helps highscore to read from the score.txt file to get the saved
 * highscores and to write to this file. If the file is removed a new file will
 * automatically be created.
 * 
 * @author Alexander
 * 
 */
public class FileWork2 {
	
	
	static final String FILENAME = "score.txt";
	static final File f = new File(FILENAME);;
	TreeSet<Pair> ia;
	
	/**
	 * constructor creates the treeset here and calls the
	 * process method to process the score.txt file at the
	 * creation of this class.
	 */
	public FileWork2(){
		ia = new TreeSet<Pair>();
		process();
	}

	
	/**
	 * This method creates a new pair with name of player and score
	 * and ads it to the treeset. Last calls the update method to 
	 * which in turn writes the new treeset to file.
	 * @param content this is the name of this player
	 * @param score this is the score of the player
	 */
	public void writeBasicDetails(String content, String score){
		Pair p = new Pair(content, Integer.parseInt(score));
		ia.add(p);
		update();
	}
	
	/**
	 * This method writes the given name and score to the file FILENAME
	 * if the file does not exist it will be created. Example:
	 * Bert 20
	 * Inger 10
	 * @param content name will be first 
	 * @param score scores will be after a space the newline
	 */
	public void writeToFile(String content, String score){
		
		if(f.exists()){
			try{
				FileWriter writer = new FileWriter(FILENAME, true);
				writer.append(content + " ");
				String newLine = System.getProperty("line.separator");
				writer.append(score);
				writer.append(newLine);
				writer.close();
			}
			catch(IOException e){
				System.err.println("Failed to save to "+FILENAME);
			}
			return;
		}else{
			try{
				FileWriter writer = new FileWriter(FILENAME);
				writer.write(content + " ");
				String newLine = System.getProperty("line.separator");
				writer.write(score);
				writer.write(newLine);
				writer.close();
			}
			catch(IOException e){
				System.err.println("Failed to save to "+FILENAME);
			}
		}
		
	}
	
	/**
	 * This reads from the file FILENAME and creates pairs for each 
	 * pair of name and score. then ads to the treeset.
	 */
	private void process(){
		Reader fis = null;
	    BufferedReader bis = null;
	    //DataInputStream dis = null;
		try {
		      fis = new FileReader(f);
		      // Here BufferedInputStream is added for fast reading.
		      bis = new BufferedReader(fis);
		      //String nextLine = bis.readLine();
		      int i = 0;
		      String line;
		      while ((line = bis.readLine()) != null && i <11) {
		    	  String[] s = line.split("\\s+");
		    	  ia.add(new Pair(s[0], Integer.parseInt(s[1])));
		    	  i++;
		      }

		      
		      // dispose all the resources after using them.
		      fis.close();
		      bis.close();

		    } catch (FileNotFoundException e) {
		      e.printStackTrace();
		    } catch (IOException e) {
		      e.printStackTrace();
		    }
		    
		    
	}
	/**
	 * This method clears the score.txt and writes the new treeset to file.
	 */
	private void update(){
	    Iterator<Pair> flavoursIter = ia.iterator();
	    
		try {
			FileWriter writer = new FileWriter(FILENAME);
			writer.write("");
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = 0;
	    while ( flavoursIter.hasNext() && i < 11){
	    	Pair pair = flavoursIter.next();
	    	writeToFile(pair.getName(), ""+pair.getScore());
	    	i++;
	    }
	}
	
	/**
	 * This method returns the scores
	 * @return returnString is the scores as a string.
	 */
	public String getScore(){
		String returnString = "";
		Iterator<Pair> flavoursIter = ia.iterator();
		int i = 0;
	    while ( flavoursIter.hasNext() && i < 11){
	    	returnString = returnString + flavoursIter.next().getScore()+ "<br />";
	    	i++;
	    }
		return returnString;
	}
	/**
	 * This method returns the names
	 * @return returnString is the names as a string.
	 */
	public String getNames(){
		String returnString = "";
		Iterator<Pair> flavoursIter = ia.iterator();
		int i = 0;
	    while ( flavoursIter.hasNext() && i < 11){
	    	returnString = returnString + flavoursIter.next().getName()+"<br />";
	    	i++;
	    }
		return returnString;
	}
}
